use actix_web::web;

use crate::apps::admin::controller::index_controller as IndexController;
use crate::apps::admin::controller::upload_controller as UploadController;
use crate::apps::admin::controller::redis_controller as RedisController;
use crate::apps::admin::controller::remote_controller as RemoteController;
use crate::apps::admin::controller::user_controller as UserController;
use crate::apps::admin::controller::role_controller as RoleController;

// 宏：注册并添加多个路由，同时打印日志信息
macro_rules! register_add_routes {
    ($config:expr, $($route:path),*) => {
        $(
            // info!("Added route: {}", stringify!($route)); // 打印日志信息，包含被添加的路由名称
            $config.service($route); // 添加路由到配置中
        )*
    };
}

// 宏：注册一个控制器下的所有路由
macro_rules! register_all_routes {
    ($config:expr, $controller:ident) => {
        register_add_routes!($config,
            $controller::detail, // 详情路由
            $controller::save,   // 保存路由
            $controller::delete, // 删除路由
            $controller::update, // 更新路由
            $controller::list    // 列表路由
        );
    };
}

// 定义API路由
pub fn api_routes(config: &mut web::ServiceConfig) {
    // 单独注册 IndexController 下的路由
    register_add_routes!(config,
        IndexController::index,
        IndexController::banner
    );

    // 单独注册 UploadController 下的路由
    register_add_routes!(config,
        UploadController::upload
    );

    // 单独注册 RemoteController 下的路由
    register_add_routes!(config,
        RemoteController::health
    );

    // 单独注册 RedisController 下的路由
    register_add_routes!(config,
        RedisController::save,
        RedisController::update,
        RedisController::delete,
        RedisController::find
    );

    // 批量注册 UserController 下的所有路由
    register_all_routes!(config, UserController);

    // 批量注册 RoleController 下的所有路由
    register_all_routes!(config, RoleController);
}
